package com.autoTools.util;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javax.swing.BoxLayout;

import org.jdom.Attribute;
import org.jdom.Element;

import com.autoTools.view.BaseView;
import com.autoTools.view.ShareViewList;

public class BuildViewUtil {
	static final String VIEWS_PACKAGE = "com.autoTools.view";
	static final String TAG = "BuildViewUtil";

	public static void buildViews(BaseView parent, Element node) {
		BaseView baseView = null;
		if (node != null) {
			List<Element> eleList = node.getChildren();

			if (!node.getName().equalsIgnoreCase("Works")) {
				baseView = createView(node.getName());
				setParams(baseView, node.getAttributes());
				ShareViewList.getInstance().putView(baseView);
				if (baseView != null && parent != null) {
					parent.addChild(baseView);
				}
			}

			if (eleList != null && eleList.size() > 0) {
				for (Element item : eleList) {
					buildViews(baseView, item);
				}
			}
		}
	}

	public static void setParams(BaseView view, List<Attribute> list) {
		if (list != null && view != null) {
			String name, value;
			for (int i = 0; i < list.size(); i++) {
				name = list.get(i).getName().trim();
				value = list.get(i).getValue().trim();
				if (list.get(i).getNamespaceURI().trim().equalsIgnoreCase("")) {
					setParamsByProperty(view, name, value);
				}else if (list.get(i).getNamespaceURI().trim()
						.equalsIgnoreCase(Constants.MARK_FUN)) {
					setParamsByFunction(view, name, value);
				}else if (list.get(i).getNamespaceURI().trim()
						.equalsIgnoreCase(Constants.MARK_ACTION)) {
					setParamsByAction(view, name, value);
				}
			}
		}
	}

	/**
	 * 获取并保存参数
	 * 
	 * @param view
	 * @param name
	 * @param value
	 */
	public static void setParamsByProperty(BaseView view, String name,
			String value) {
		if (view != null && name != null && value != null) {
			view.getProperty().put(name, value);
			if (name.equalsIgnoreCase(Constants.LAYOUT)) {
				setLayout(view, name, value);
			} else if (name.equalsIgnoreCase(Constants.ID)) {
				setId(view, name, value);
			} else if (name.contains(Constants.COLUMN)) {
				setColumn(view, name, value);
			}
			
		}
	}
	
	/**
	 * 注册事件
	 * @param view
	 * @param name
	 * @param value
	 */
	public static void setParamsByAction(BaseView view, String name,
			String value) {
		if (view != null && name != null && value != null) {
			view.setAction(name,value);
		}
	}
	
	/**
	 * �������Function���ò���
	 * 
	 * @param view
	 * @param name
	 * @param value
	 */
	public static void setParamsByFunction(BaseView view, String name,
			String value) {
		ReflectParam reflectParam = null;
		reflectParam = ClassUtil.getInstance().getReflectParam(value);
		Method method;
		try {
			method = view.getInstance().getClass()
					.getMethod(name, reflectParam.getMclass());
			method.invoke(view.getInstance(), reflectParam.getMObjs());
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setId(BaseView view, String name, String value) {
		if (view != null && name != null && value != null) {
			view.setLabel(value);
		}
	}

	public static void setLayout(BaseView view, String name, String value) {
		if (view != null && name != null && value != null) {
			LayoutManager layout = null;
			if (value.equalsIgnoreCase(Constants.CARD_LAYOUT)) {
				layout = new CardLayout();
			} else if (value.equalsIgnoreCase(Constants.BORDER_LAYOUT)) {
				layout = new BorderLayout();
			} else if (value.equalsIgnoreCase(Constants.FLOW_LAYOUT)) {
				layout = new FlowLayout();
			} else if (value.equalsIgnoreCase(Constants.GRIDBAG_LAYOUT)) {
				layout = new GridBagLayout();
			} else if (value.equalsIgnoreCase(Constants.GRID_LAYOUT)) {
				layout = new GridLayout();
			} else if (value.equalsIgnoreCase(Constants.NULL_LAYOUT)) {
				layout = null;
			}else if (value.equalsIgnoreCase(Constants.BOX_X_LAYOUT)) {
				layout = new BoxLayout((Container) view.getInstance(),BoxLayout.X_AXIS);
			}else if (value.equalsIgnoreCase(Constants.BOX_Y_LAYOUT)) {
				layout = new BoxLayout((Container) view.getInstance(),BoxLayout.Y_AXIS);
			}
			if (view.getInstance() instanceof Container) {
				((Container) view.getInstance()).setLayout(layout);
			}
		}
	}
	
	public static void setColumn(BaseView view, String name, String value) {
		view.setColumn(value);
	}
	
	public static BaseView createView(String viewName) {
		BaseView baseView = null;
		if (viewName != null) {
			Class<?> viewObj = null;
			try {
				viewObj = Class.forName(VIEWS_PACKAGE + "." + viewName);
				baseView = (BaseView) viewObj.newInstance();
				LogUtil.sysLog(TAG, "create view success");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return baseView;
	}

}
